package com.pinyoumei.app.activity.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.pinyoumei.app.R;
import com.pinyoumei.app.activity.BaseActivityCurrency;
import com.pinyoumei.app.activity.MainUIActivity;
import com.pinyoumei.app.activity.integral.IntegralShopActivity;
import com.pinyoumei.app.network.Constants;
import com.pinyoumei.app.utils.EventBusUtils;
import com.pinyoumei.app.utils.SPUtils;
import com.pinyoumei.app.utils.SignatureUtils;
import com.pinyoumei.app.utils.Utils;
import com.pinyoumei.app.view.SlidingTabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WalletTransactionActivity extends BaseActivityCurrency {

    private SlidingTabLayout tl_1;
    private final static String[] TITLE_1 = {"收入明细","支出明细","提现明细"};
    private ArrayList<Fragment> mFragments_1 = new ArrayList<>();
    private ViewPager vp_1;
    private int intentType = 0;//1:余额,2:开心团，3：积分
    private TextView wtvMoney,wtvTimeTitle,wtvhappyGroup,wtvHappyTwo;
    private String intentNum = "";

    @Override
    protected int layoutId() {
        return R.layout.wallet_transaction_view;
    }

    @Override
    protected void initView() {
        Bundle bundle = getIntent().getExtras();
        intentType = bundle.getInt("intentType");
        intentNum = bundle.getString("intentNum");

        tl_1 = findViewById(R.id.tl_1);
        vp_1 = findViewById(R.id.vp_1);
        wtvMoney = findViewById(R.id.wtv_money);
        wtvTimeTitle = findViewById(R.id.wtv_time_title);
        wtvhappyGroup = findViewById(R.id.wtv_happy_group);
        wtvHappyTwo = findViewById(R.id.wtv_happy_two);
        if(intentType == 2){
            wtvTimeTitle.setText("开心团劵");
            wtvhappyGroup.setText("前往开心团");
            wtvHappyTwo.setText("转积分");
        }else if (intentType == 3){
            wtvTimeTitle.setText("积分");
            wtvhappyGroup.setText("前往开心团");
            wtvHappyTwo.setText("兑换商品");
            wtvhappyGroup.setVisibility(View.GONE);
        }

        mFragments_1.add(WalletIncomeFragment.getInstance(intentType));
        mFragments_1.add(WalletExpenditureFragment.getInstance(intentType));
        if(intentType == 1){
            mFragments_1.add(WithdrawalObviousFragment.getInstance(intentType));
        }

        MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
        vp_1.setAdapter(mAdapter);
        tl_1.setViewPager(vp_1);
    }

    @Override
    protected void initData() {
        wtvhappyGroup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (intentType == 1){
                    startActivityForIntent(WalletHappyGroupActivity.class);
                }else if (intentType == 2){
                    MainUIActivity.getInstance.Select_Position(1);
                    EventBusUtils.post("finsh");
                    finish();
                }

            }
        });
        findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        wtvHappyTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (intentType == 1){
                    startActivityForIntent(RechargeActivity.class);
                }else if (intentType == 2){
//                    buyHappyCouponInit(SPUtils.getAppUserId(WalletTransactionActivity.this));
                    startActivityForIntent(HappyCouponToScoreInitActivity.class);
                }else if (intentType == 3){
                    startActivityForIntent(IntegralShopActivity.class);
                }
            }
        });
        findViewById(R.id.wtv_time_select).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new TimeSelectDialog(WalletTransactionActivity.this, 0, new int[]{}).show();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        myWalletRequest(SPUtils.getAppUserId(this));
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments_1.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLE_1[position];
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments_1.get(position);
        }
    }

    private void buyHappyCouponInit(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.buyHappyCouponInit)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        HappyCouponInitBean happyCouponInitBean = new Gson().fromJson(response.body(), HappyCouponInitBean.class);
                        Utils.red_package_amount = Double.parseDouble(happyCouponInitBean.getResult().getRed_package_amount());
                        Utils.happy_coupon = happyCouponInitBean.getResult().getHappy_coupon();
                        if(happyCouponInitBean.getCode() == 2){
                            startActivityForIntent(WalletRechargeActivity.class);
                        }else {
                            new WalletCouponDialog(WalletTransactionActivity.this,0,new int[]{}).show();
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                    }
                });
    }

    private void myWalletRequest(String user_id){
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",user_id);
        OkGo.<String>post(Constants.CENTER_MYWALLET)
                .params("user_id", user_id)
                .params("sign", SignatureUtils.createSignature(map, Constants.SIGN_SECRET))
                .execute(new StringCallback() {

                    @Override
                    public void onStart(Request<String, ? extends Request> request) {
                        super.onStart(request);
                        loadingDailog.show();
                    }

                    @Override
                    public void onSuccess(Response<String> response) {
                        loadingDailog.dismiss();
                        CenterWalletBean centerWalletBean = gson.fromJson(response.body(), CenterWalletBean.class);
                        if(intentType == 1){
                            wtvMoney.setText(centerWalletBean.getResult().getData_info().getUser_balance());
                        }else if (intentType == 2){
                            wtvMoney.setText(centerWalletBean.getResult().getData_info().getHappy_coupon());
                        }else if (intentType == 3){
                            wtvMoney.setText(centerWalletBean.getResult().getData_info().getUser_score());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        loadingDailog.dismiss();
                    }
                });
    }

}
